package com.hey.html.controller;

import com.hey.html.entity.Baoming;
import com.hey.html.respository.HtmlRepository;
import enums.CodeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import result.MultiResult;
import result.SingleResult;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@CrossOrigin(origins = "*")
@Controller
public class MainController {

    @Autowired
    HtmlRepository htmlRepository;

    @PostMapping("save")
    @ResponseBody
    public SingleResult save(@RequestBody Baoming baoming){
        SingleResult result = new SingleResult();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-mm-dd HH:MM:ss");
        try {
            baoming.setCreateTime(sdf.format(new Date()));
            htmlRepository.save(baoming);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg(CodeStatus.ERROR);
            return result;
        }
    }

    @GetMapping("get")
    @ResponseBody
    public MultiResult get(){
        MultiResult result = new MultiResult();
//        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-mm-dd HH:MM:ss");
        try {
            List<Baoming> list = htmlRepository.findAll();
//            list.forEach(l->l.setCreateTime(sdf.format(l.getCreateTime())));
            result.setData(list);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg(CodeStatus.ERROR);
            return result;
        }
    }
}
